<?php

if (!defined('PUBLIC_HTML'))
{
	exit;
}

$id = (int) $_GET['id'];

if ($id == 0 || !is_numeric($id))
{
	redirect('cms.php?p=pages');
	exit;
}

if (!$db->sql_count(TABLE_PAGES, 'page_id', $id) == 1)
{
	redirect('cms.php?p=pages');
	exit;
}

// Define language
$language = (isset($_GET['lang'])) ? $_GET['lang'] : 'en';

// Define id submission
if (isset($_GET['edit']) && isset($_GET['edit_id']))
{
	$submit_type 	= $_GET['edit'];
	$submit_id		= $_GET['edit_id'];
	switch ($submit_type)
	{
		case 'page':
			$post = array();
			$post['page_mod_id'] 	= 0;
			$post['page_parent_id'] = 0;
			$post['page_title']		= $db->sql_injection($_POST['title']); 
			$post['page_full']		= 1;
			$post['page_type']		= 1;
			$db->sql_update_row(TABLE_PAGES, $post, 'page_id', $id);
			redirect('cms.php?p=pages&act=edit&id='.$id);
			exit;
		break;
		
		case 'content':
			$post = array();
			$post['pct_var']		= strtolower($_POST[$edit_id . '_content_title']);
			$post['pct_type']		= 'default';
			$post['pct_title']		= $_POST[$edit_id . '_content_title'];
			$post['pct_content']	= $_POST[$edit_id . '_content'];
			$db->sql_update_row(TABLE_PAGES_CONTENT, $post, 'pct_id', $submit_id);
			redirect('cms.php?p=pages&act=edit&id='.$id);
			exit;
		break;
		
		case 'language':
			$post = array();
			$post['pla_page_id']	= $row_added_page[$edit_id . 'page_id'];
			$post['pla_lang']		= $language;
			$post['pla_var']		= strtolower($_POST[$edit_id . 'page_title']);
			$post['pla_title']		= (string) $_POST[$edit_id . 'page_title'];
			$post['pla_description']= (string) $_POST[$edit_id . 'description'];
			$post['pla_keywords']	= (string) $_POST[$edit_id . 'keywords'];
			$db->sql_insert_row(TABLE_PAGES_LANG, $post, 'pla_id', $submit_id);
		break;
	}
}
elseif (isset($_GET['add']) && isset($_GET['add_id']))
{
	$submit_type	= $_GET['add'];
	$submit_id		= $_GET['add_id'];
	switch ($submit_type)
	{
		case 'content':
			$post = array();
			$post['pct_page_id'] 	= $row_added_page['page_id'];
			$post['pct_lang']		= $language;
			$post['pct_var']		= strtolower($_POST['page_title']);
			$post['pct_type']		= 'default';
			$post['pct_title']		= $_POST['page_title'];
			$post['pct_content']	= $_POST['content'];
			$post['pct_module']		= '';
			$post['pct_module_part']= '';
			$post['pct_row']		= '1';
			$post['pct_rowspan']	= '1';
			$post['pct_col']		= '1';
			$post['pct_colspan']	= '1';
			$post['pct_position']	= '1';
			$db->sql_insert_row(TABLE_PAGES_CONTENT, $post);
			//redirect('cms.php?p=pages');
			exit;
		break;
		
		case 'language':
			$post = array();
			$post['pla_page_id']	= $submit_id;
			$post['pla_lang']		= 'en';
			$post['pla_var']		= strtolower($_POST['page_title']);
			$post['pla_title']		= (string) $_POST['page_title'];
			$post['pla_description']= (string) $_POST['description'];
			$post['pla_keywords']	= (string) $_POST['keywords'];
			$db->sql_insert_row(TABLE_PAGES_LANG, $post);
		break;
	}
}

// Submission
$submit = '';

// Get available languages for this page
$get_a_languages =	"
					SELECT	pla_page_id,pla_lang
					FROM	" . TABLE_PAGES_LANG . "
					WHERE	pla_page_id = '" . $id ."'
					";
$sql_a_languages =	$db->sql_query($get_a_languages);
while ($row_a_languages = $db->sql_fetch_assoc($sql_a_languages))
{
	$tpl->zone('languages', array(
		 'name'		=> (string) $row_a_languages['pla_lang'],
		 'current'	=> ($row_a_languages['pla_lang'] == $language) ? ' selected="selected"' : ''
	));
}

// Get main page information
$get_page =	"
			SELECT		*
			FROM		" . TABLE_PAGES . "
			WHERE		page_id = '" . $id . "'
			LIMIT		0,1
			";
$sql_page = $db->sql_query($get_page);
$row_page = $db->sql_fetch_assoc($sql_page);
$tpl->set_vars('PAGE_TITLE', (string) $row_page['page_title']);

// Get page language specific settings
$get_language =	"
				SELECT		*
				FROM		" . TABLE_PAGES_LANG . "
				WHERE		pla_page_id = '" . $id . "'
				AND			pla_lang = '" . $language . "'
				";
$sql_language =	$db->sql_query($get_language);
while ($row_language = $db->sql_fetch_assoc($sql_language))
{
	$tpl->zone('language', array(
		'title'			=> $row_language['pla_title'],
		'description'	=> $row_language['pla_description'],
		'keywords'		=> $row_language['pla_keywords'],
		'content'		=> $row_language['pla_content'],
	));
}

// Get page contents
$get_content =	"
				SELECT		*
				FROM		" . TABLE_PAGES_CONTENT . "
				WHERE		pct_page_id = '" . $id . "'
				AND			pct_lang = '" . $language . "'
				ORDER BY	pct_position
				ASC
				";
$sql_content =	$db->sql_query($get_content);
while ($row_content = $db->sql_fetch_assoc($sql_content))
{
	$tpl->zone('content', array(
		'var'		=> $row_content['pct_var'],
		'type'		=> $row_content['pct_type'],
		'title'		=> $row_content['pct_title'],
		'content'	=> nl2br($row_content['pct_content']),
		'position'	=> $row_content['pct_position']
	));
}

// Get Pages for the Subpage dropdown
foreach ($db->sql_get(TABLE_PAGES, '*', 'page_parent_id', 0) as $row_pages)
{
	$tpl->zone('pages', array(
		'ID'		=> (int)	$row_pages['page_id'],
		'TITLE'		=> (string)	$row_pages['page_title'],
		'SELECTED'	=> ($row_pages['page_parent_id'] == $row_page['page_parent_id']) ? 'true' : 'false'
	));
}

?>